
/*

DEPENDENCIES:
-------------
Gimme.Effects
ext.get_style
ext.filter
-------------


IN THIS FILE:
-------------
ext.veil
-------------

*/

Gimme.ext.veil = function(direction, duration, guid, callback, easing)
{
	var D = Gimme.Effects.Enums.Directions;
	direction = Math.floor(direction) || D[direction] || D.vertically;
	
	var expando = '_$gimme$_veil';
	
	// Save important values in an expando so they can be used for unveil (if needed):			
	// height, width, paddingTop, paddingBottom, paddingRight, paddingLeft
	function getFrom(elem, prop)
	{
		var v = parseInt(Gimme(elem).get_style(prop), 10);
		elem[expando] = elem[expando] || {};
		return elem[expando][prop] = v;				
	}
	
	var to = {};
	var from = {};
	if ((direction & D.vertically) === D.vertically)
	{
		from.height = from.paddingTop = from.paddingBottom = getFrom;
		to.height = to.paddingTop = to.paddingBottom = 0;
	}
	if ((direction & D.horizontally) === D.horizontally)
	{
		from.width = from.paddingLeft = from.paddingRight = getFrom;
		to.width = to.paddingLeft = to.paddingRight = 0;
	}
	
	var sb = Gimme.Effects.Storyboard.create
	(
		// elements to be animated
		this.filter(function(elem)
		{
			var display = elem.style.display || Gimme(elem).get_style('display');
			return display !== 'none';
		}),
		
		// from
		from,
		
		// to
		to,
		
		//animate function
		function(elem, props)
		{
			var elemStyle = elem.style;
			if ((direction & D.vertically) === D.vertically)
			{
				elemStyle.height = props.height + 'px';
				elemStyle.paddingTop = props.paddingTop + 'px';
				elemStyle.paddingBottom = props.paddingBottom + 'px';
			}
			if ((direction & D.horizontally) === D.horizontally)
			{
				elemStyle.width = props.width + 'px';
				elemStyle.paddingRight = props.paddingRight + 'px';
				elemStyle.paddingLeft = props.paddingLeft + 'px';
			}
		},
		
		//duration
		duration,
		
		//guid
		guid,
		
		//callback
		callback,
		
		//easing
		easing || function(t)
		{
			return Gimme.Effects.Easing.Exponential.ease_in_out(t, 4);
		}
	);
	
	// add a discrete animation for setting overflow to hidden
	sb.get_animations().push
	(
		new Gimme.Effects.Animation(
		{
			startTime: 0,
			elements: this,
			animate: function(elem)
			{
				// Make sure content isn't bleeding over its container during the veiling process				
				elem.style.overflow = 'hidden';
			}
		}),
		
		new Gimme.Effects.Animation(
		{
			startTime: sb.get_duration(),
			elements: this,
			animate: function(elem)
			{
				// bye bye
				elem.style.display = 'none';
			}
		})
	);
	
	// start the animation storyboard			
	sb.begin();
	return this;
};